package com.wl.shixun5.dao;

import com.wl.shixun5.info.BookInfo;
import com.wl.shixun5.util.Bookjdbc_Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 图书信息DAO类
 */
public class BookInfo_DAO {
    final static int PAGESIZE = 3; // 每页显示3条记录

    /**
     * 分页查询图书列表
     * @param page 当前页码
     * @return 图书列表
     */
    public static List<BookInfo> find(int page) {
        List<BookInfo> li = new ArrayList<BookInfo>();
        String sql = "select * from BookInfo limit ?,?";
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setInt(1, (page - 1) * PAGESIZE);
            ps.setInt(2, PAGESIZE);
            ResultSet rs;
            rs = ps.executeQuery();
            while (rs.next()) {
                BookInfo bi = new BookInfo();
                bi.setBookAuthor(rs.getString("bookAuthor"));
                bi.setBookISBN(rs.getString("bookISBN"));
                bi.setBookName(rs.getString("bookName"));
                bi.setId(rs.getInt("Id"));
                bi.setIntroduce(rs.getString("introduce"));
                bi.setPrice(rs.getFloat("price"));
                bi.setPublisher(rs.getString("publisher"));
                li.add(bi);
            }

            if (rs != null) {rs.close();}
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return li;
    }

    /**
     * 获取图书总记录数
     * @return 记录数
     */
    public static int getRecordCount() {
        int c = 0;
        String sql = "select count(*) from BookInfo";
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ResultSet rs;
            rs = ps.executeQuery();
            if (rs.next()) {
                c = rs.getInt(1);
            }
            if (rs != null) {rs.close();}
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }

    /**
     * 计算总页数
     * @return 总页数
     */
    public static int yeshu() {
        int c = getRecordCount();
        return c % PAGESIZE == 0 ? c / PAGESIZE : (c / PAGESIZE + 1);
    }

    /**
     * 根据ID查询图书详细信息
     * @param Id 图书ID
     * @return 图书信息
     */
    public static BookInfo findId(String Id) {
        String sql = "select * from BookInfo where Id=?";
        BookInfo bi = new BookInfo();
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, Id);
            ResultSet rs;
            rs = ps.executeQuery();
            if (rs.next()) {
                bi.setBookAuthor(rs.getString("bookAuthor"));
                bi.setBookISBN(rs.getString("bookISBN"));
                bi.setBookName(rs.getString("bookName"));
                bi.setId(rs.getInt("Id"));
                bi.setIntroduce(rs.getString("introduce"));
                bi.setPrice(rs.getFloat("price"));
                bi.setPublisher(rs.getString("publisher"));
            }

            if (rs != null) {rs.close();}
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bi;
    }
}
